# 爬取一部小说所有内容

import asyncio
import aiohttp
import requests
import json

# https://novelapi.baidu.com/boxnovel/cors?uid=&boxnovelTimeStampNow=1679035572633&trace_log=undefined&action=novel&type=content&osname=wise&data={"book_id":bid,"chapter_id":"1569782245"}

bid = '4306063500'

async def aioDownload(title, cid):
    data = {
        "book_id": bid,
        "chapter_id": cid
    }
    data = json.dumps(data)
    url = f'https://novelapi.baidu.com/boxnovel/cors?uid=&boxnovelTimeStampNow=1679035572633&trace_log=undefined&action=novel&type=content&osname=wise&data={data}'
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            dic = await resp.json()
            downloadUrl = dic['data']['url']
            print(title, downloadUrl)

async def getCatalog(url):
    resp = requests.get(url)
    dic = resp.json()
    tasks = []
    for chapter in dic['data']['chapter']['chapterInfo']:
        title = chapter['chapter_title']
        cid = chapter['chapter_id']
        print(title, cid)
        tasks.append(aioDownload(title, cid))

    await asyncio.wait(tasks)

if __name__ == '__main__':
    url = 'https://boxnovel.baidu.com/boxnovel/wiseapi/chapterList?bookid=' + bid + '&pageNum=1&order=asc&site='
    # asyncio.run(getCatalog(url))
    loop = asyncio.get_event_loop()
    loop.run_until_complete(getCatalog(url))